<?php

	$action = fgsBase::getParam(0);
	$id = (int)fgsBase::getParam(1);
	$form = null;

	// ---------------------------------------------------------------
	// ADD or EDIT a news 
	// ---------------------------------------------------------------
	if ($action == 'add' || $action == 'edit') {
		$form = new htmlForm('news');
		
		if ($form->isCancel())
			fgsBase::redirect('admin_news');
		
		if ($action == 'add') {
			$form->addSection(tr('Add a news'));
		} else {
			$news = fgsNews::get($id);
			if (!$news)
				fgsBase::redirect('admin_news');
			$form->addSection(sprintf(tr('Edit news #%d'), $id));

			$form->default_values['cat'] = $news['category'];
			$form->default_values['lang'] = $news['lang'];
			$form->default_values['title'] = $news['title'];
			$form->default_values['content'] = $news['text'];
			$form->default_values['online'] = $news['is_online'] ? 1 : 0;
		}

		$lang = fgsLang::getList();
		$opts_lang = array();
		foreach ($lang as $k=>$v) {
			$opts_lang[] = array(
				'id' => $k,
				'title' => $v,
			);
		}

		$cats = fgsNews::getCategories();
		$opts_cats = array();
		foreach ($cats as $k=>$v) {
			$opts_cats[] = array( 
				'id' => $v['id'],
				'title' => $v['name']
			);
		}

		$form->addSelect(tr('Language'), 'lang', true, $opts_lang);
		$form->addSelect(tr('Category'), 'cat', true, $opts_cats);
		$form->addField(tr('Title'), 'title', 'text', true);
		$form->addField(tr('Content'), 'content', 'textarea', true);
		$form->addField(tr('Publish'), 'online', 'checkbox');
		$form->addSubmit(tr('Submit'));

		// add news if post is good
		if ($form->isPost() && $form->isValid()) {
			$lang = $form->get('lang');
			$cat = $form->get('cat');
			$title = $form->get('title');
			$content = $form->get('content');
			$isonline = $form->get('online') ? true : false;
			
			if ($action == 'add')
				fgsNews::add($title, '', $cat, $content, $lang, 0, $isonline);
			else 
				fgsNews::update($id, $title, '', $cat, $content, $lang, 0, $isonline);

			fgsBase::redirect('admin_news');
		}


	// ---------------------------------------------------------------
	// SHOW a news
	// ---------------------------------------------------------------
	} else if ($action == 'show') {
		fgsNews::makeOnline($id, 1);
		fgsBase::redirect('admin_news');
		
	// ---------------------------------------------------------------
	// HIDE a news
	// ---------------------------------------------------------------
	} else if ($action == 'hide') {
		fgsNews::makeOnline($id, 0);
		fgsBase::redirect('admin_news');

	}
	
?>
